/**
 * vivo
 *
 * @author   songhuan <iphper@yeah.net>
 * @version   $Id: core-1.1.1.js 1 2010-08-10 14:40:00Z $
 */
vivo = {
	//判断浏览器类型
	browserType: function() {
		var userAgent = navigator.userAgent.toLowerCase();
		if (userAgent.indexOf('opera') != -1 && opera.version()) {
			return "opera";
		}
		//Mozilla Firefox
		else if ((navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3)) {
			return "moz";
		}
		//IE，遨游，搜狗
		else if (userAgent.indexOf('msie') != -1 && userAgent.substr(userAgent.indexOf('msie') + 5, 3)) {
			return "ie";
		}
		else if (userAgent.indexOf('mac') != -1) {
			return "mac";
		}
		return "other";
	},
	v: function(element) {
		return typeof(element) == 'object' ? element : document.getElementById(element);
	},
	ltrim: function(E) {
		return (E||"").replace(/^\s+/g,"");
	},
	rtrim: function(E) {
		return (E||"").replace(/\s+$/g,"");
	},
	trim: function(E) {
		return (E||"").replace(/^\s+|\s+$/g,"");
	},
	mb_strlen: function(E, charset) {
		var len = 0;
		var Elen = E.length;
		for (var i = 0; i < Elen; i++) {
			len += E.charCodeAt(i) < 0 || E.charCodeAt(i) > 255 ? (charset == "utf-8" ? 3 : 2) : 1;
		}
		return len;
	},
	mb_cutstr: function(E, maxlen, charset) {
		var len = 0;
		var ret = "";
		var Elen = E.length;
		for (var i = 0; i < Elen; i++) {
			len += E.charCodeAt(i) < 0 || E.charCodeAt(i) > 255 ? (charset == "utf-8" ? 3 : 2) : 1;
			if(len > maxlen) {
				break;
			}
			ret += E.substr(i, 1);
		}
		return ret;
	},
	ucfirst: function(E) {
		return E.substr(0, 1).toUpperCase() + E.substr(1).toLowerCase();
	},
	in_array: function(needle, haystack) {
		if (typeof needle == 'string' || typeof needle == 'number') {
			for (var i in haystack) {
				if(haystack[i] == needle) {
					return true;
				}
			}
		}
		return false;
	},
	//判断是否是闰年
	isLeapYear: function(year) {
		 return (0==year%4&&((year%100!=0)||(year%400==0)));
	},
	validEmail: function(E) {
		var pattern = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/ ;
		if (E == "" || !pattern.test(E)) {
			return false;
		}
		return true;
	},
	//判断生日的年份是否合理，年份区间：1901 - 当前年
	validYear: function(year) {
		if (year == "") return 0;
		if ( /^[0-9]+$/.test(year) && year <= new Date().getFullYear() && year > 0 && parseInt(year) >= 1901) return 1;
		return -1;
	},
	validMonth: function(month) {
		if (month == "") return 0;
		if ( /^[0-9]+$/.test(month) && month <= 12 && month > 0) return 1;
		return -1;
	},
	validDay: function(year, month, day) {
		if (day == "") return 0;
		maxday = 31;
		if (month != null || month != "") {
			switch (parseInt(month)) {
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:
					maxday = 31; break;
				case 2:
					if(isLeapYear(year)) maxday = 29;
					else maxday = 28;
					break;
				case 4:
				case 6:
				case 9:
				case 11:
					maxday = 30; break;
				default:
					maxday=31;
			}
		}
		if ( /^[0-9]+$/.test(day) && day <= maxday && day > 0) return 1;
		return -1;
	},
	//静态页面获取参数
	queryString: function() {
		var request_uri = window.location.href;
		var strpos = request_uri.indexOf("?") + 1;
		if (strpos == 0) {
			return Array();
		}
		var query_string = request_uri.substr(strpos);
		if (query_string.length == 0) {
			return Array();
		}
		var temp = new Array(2);
		if (query_string.indexOf("&") == -1) {
			var _GET = new Array(1);
			var temp = query_string.split("=");
			_GET[temp[0]] = temp[1];
			return _GET;
		}
		var query_strings = query_string.split("&");
		var _GET = new Array(query_strings.length);
		for (i in query_strings) {
			temp = query_strings[i].split("=");
			_GET[temp[0]] = temp[1];
		}
		return _GET;
	},
	getCookie: function(name) {
		var cookies = document.cookie.split("; ");
		for (var i in cookies) {
			var crumbs = cookies[i].split("=");
			if (name == crumbs[0])
				return decodeURIComponent(crumbs[1]);
		}
		return null;
	},
	setCookie: function(name, value, expires) {
		var cookies = name + "=" + encodeURIComponent(value);
		if (expires != null) {
			cookies += "; expires=" + expires;
		}
		document.cookie = cookies;
	},
	removeCookie: function(name) {
		document.cookie = name + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
	},
	/**
	 * Ajax
	 * @param json p
	 * p = {type: "GET"|"POST", url: "", data: "", async: true|false, success: function() {}}
	 */
	ajax: function(p) {
		var xhrObj = false;

		try {
			xhrObj = new XMLHttpRequest(); // Firefox IE8和非IE内核浏览器
		}
		catch (e) {
			var progid = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"]; // IE5.5 IE6 IE7内核
			for (var i in progid) {
				try {
					xhrObj = new ActiveXObject(progid[i]);
				}
				catch (e) {
					continue;
				}
				break;
			}
		}

		p.type = p.type.toUpperCase();

		if ("POST" != p.type) {
			p.type = "GET";
			p.url.indexOf("?") == -1 ? p.url += "?" + p.data : "";
			p.data = null;
		}

		false != p.async ? p.async = true : "";
		xhrObj.open(p.type, p.url, p.async);
		if ("POST" == p.type) {
			xhrObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		}
		xhrObj.send(p.data);

		xhrObj.onreadystatechange = function() {
			/**
			 * readyState 值
			 * 2 已经发送数据了，但是还没接收到反馈
			 * 3 收到反馈了，反馈描述数据正在发送的过程中
			 * 4 反馈描述数据已经被接收完毕
			 */
			if (xhrObj.readyState == 4) {
				if (xhrObj.status == 200) {
					return p.success(xhrObj.responseText);
				}
			}
		} 
	}
}